#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "MyString.h"
#include "DynamicArray.h"
#include "Tool.h"

struct Coin *CreateCoin(const char *name, int value)
{
    struct Coin *coin = (struct Coin *)malloc(sizeof(struct Coin));
    if (coin == NULL)
    {
        printf("Create Coin Error!\n");
        return NULL;
    }
    coin->value = value;
    Initiallize(&coin->name, name);

    coin->x = GetRandNumber(Map_Size);
    coin->y = GetRandNumber(Map_Size);
    while (coin->x == 0 && coin->y == 0)
    {
        coin->x = GetRandNumber(Map_Size);
        coin->y = GetRandNumber(Map_Size);
    }

    return coin;
}

struct LuckyWheel *CreateLWheel(const char *name, int HP, int AP, int Value)
{
    struct LuckyWheel *LWheel = (struct LuckyWheel *)malloc(sizeof(struct LuckyWheel));
    if (LWheel == NULL)
    {
        printf("Create LuckyWheel Error!\n");
        return NULL;
    }
    LWheel->AP = AP;
    LWheel->HP = HP;
    LWheel->Value = Value;
    Initiallize(&LWheel->name, name);

    return LWheel;
}

void InitLuckyWheel(struct DynamicArray *array)
{
    if (ArrayInit(array) == false)
    {
        return;
    }
    srand(time(NULL));

    InsertArray(array, CreateLWheel("空宝箱", 0, 0, 0));
    InsertArray(array, CreateLWheel("大逼兜", 0, 0, 0));
    InsertArray(array, CreateLWheel("一张用过的废纸", 0, 0, 0));
    InsertArray(array, CreateLWheel("王涵涵扔的垃圾", 0, 0, 0));
    InsertArray(array, CreateLWheel("王涵涵扔的垃圾", 0, 0, 0));
    InsertArray(array, CreateLWheel("王涵涵扔的垃圾", 0, 0, 0));
    InsertArray(array, CreateLWheel("高启胜的咸鱼", 0, 1, 0));
    InsertArray(array, CreateLWheel("鸡毛掸子", 0, 1, 0));
    InsertArray(array, CreateLWheel("降龙十八掌", 0, 12, 0));
    InsertArray(array, CreateLWheel("疯狂之爪", 0, 5, 0));
    InsertArray(array, CreateLWheel("恶魔果实", 10, 5, 0));
    InsertArray(array, CreateLWheel("福音圣水", 30, 0, 0));
    InsertArray(array, CreateLWheel("猪肉满满", 10, 0, 0));
    InsertArray(array, CreateLWheel("骑士铁血心", 5, 5, 0));
    InsertArray(array, CreateLWheel("仙女魔法防御", 5, 1, 0));
    InsertArray(array, CreateLWheel("幽默宝箱", 0, 0, 15));
    InsertArray(array, CreateLWheel("古怪胡萝卜宝石", 0, 0, 10));
    InsertArray(array, CreateLWheel("淘气宝贝手链", 0, 0, 5));
    InsertArray(array, CreateLWheel("猫粮大礼包", 5, 0, 0));
    InsertArray(array, CreateLWheel("傻瓜保护罩", 15, 1, 0));
    // 20
}

void InitCoin(struct DynamicArray *array)
{
    if (ArrayInit(array) == false)
    {
        return;
    }

    InsertArray(array, CreateCoin("金币", 5));
    InsertArray(array, CreateCoin("金币", 5));
    InsertArray(array, CreateCoin("银币", 3));
    InsertArray(array, CreateCoin("银币", 3));
    InsertArray(array, CreateCoin("银币", 3));
    InsertArray(array, CreateCoin("铜币", 1));
    InsertArray(array, CreateCoin("铜币", 1));
    InsertArray(array, CreateCoin("铜币", 1));
    InsertArray(array, CreateCoin("铜币", 1));
    InsertArray(array, CreateCoin("铜币", 1));
}

int Luckydraw(struct Player *player)
{
    system("clear");
    printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(^_^)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
    printf("小金库还有%d坤\n", player->pocket);
    sleep(1);
    if(player->pocket >= 0)
    {
        printf("小哥哥快来吖，幸运大转盘要不要试一试,只要5坤钱!!!!!!\n");
        sleep(1);
        printf("请选择\n");
        sleep(1);
        printf("\n");
        printf("1、搏一搏单车变摩托,我要试一试\n");
        printf("2、我妈妈不让我玩\n");
        int choice;
        scanf("%d", &choice);
        getchar();
        switch (choice)
        {
        case 1:
            if (GetRandNumber(2) == 1)
            {
                printf("你做了个明智的选择，幸运女神会保佑你\n");
                if (player->pocket >= 5)
                {
                    player->pocket -= 5;
                    struct DynamicArray LuckyWheels = {NULL, 20, 0};
                    InitLuckyWheel(&LuckyWheels);
                        struct LuckyWheel *result = GetRandomLWheel(&LuckyWheels);
                        printf("恭喜你抽到了：%s\n", result->name.string);
                        printf("增益血量:%d,攻击力%d,财宝%d坤\n",result->HP,result->AP,result->Value);
                        player->attack += result->AP;
                        player->HP += result->HP;
                        player->pocket += result->Value;
                    printf("目前的血量:%d,攻击力: %d,小金库 : %d\n", player->HP,player->attack,player->pocket);
                    FreeArray(&LuckyWheels);
                    break;
                }
                else
                {
                    printf("oh shit,就这么一点钱还来这,快滚吧\n");
                    break;
                }
                break;
            }
            else
            {
                printf("哇哈哈哈，不要贪杯哦\n");
                if (player->pocket >= 5)
                {
                    player->pocket -= 5;
                    struct DynamicArray LuckyWheels = {NULL, 20, 0};
                    InitLuckyWheel(&LuckyWheels);
                        struct LuckyWheel *result = GetRandomLWheel(&LuckyWheels);
                        printf("恭喜你抽到了：%s\n", result->name.string);
                        printf("增益血量:%d,攻击力%d,财宝%d坤\n",result->HP,result->AP,result->Value);
                        player->attack += result->AP;
                        player->HP += result->HP;
                        player->pocket += result->Value;
                    printf("目前的血量:%d,攻击力: %d,小金库 : %d\n", player->HP,player->attack,player->pocket);
                    FreeArray(&LuckyWheels);
                    break;
                }
                else
                {
                    printf("穷鬼连抽奖的资格都没有,快滚吧\n");
                    break;
                }
                break;
            }
            break;
        case 2:
            if (GetRandNumber(4) == 1)
            {
                printf("你妈妈总有一天会同意的！！\n");
                break;
            }
            else if (GetRandNumber(4) == 2)
            {
                printf("王涵涵画了个圈圈并诅咒了你！！\n");
                break;
            }
            else if (GetRandNumber(4) == 3)
            {
                printf("欢迎您再次光临！！\n");
                break;
            }
            else
            {
                printf("oh你个吝啬鬼,早晚有一天你会后悔！！\n");
                break;
            }
        default:
            break;
        }
    }
    printf("=======================================end==============================================\n");
    sleep(2);
}